#include "do_camera.h"
#include "convert.h"
struct v4l2_requestbuffers reqbuf;
VideoBuffer * framebuf;
struct v4l2_buffer buf;
static RGB24 rgb24;
int h = 480,w = 640,bps = 24;
int cammerFd;

int do_camera1(int cfd)
{
	initcamera(&cammerFd);
	
	fenpei(&reqbuf, &cammerFd);
	
	framebuf = calloc(reqbuf.count, sizeof(*framebuf));
	int i;
	for (i = 0; i < reqbuf.count; i++) 
    {
        buf.index = i;
        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        buf.memory = V4L2_MEMORY_MMAP;
        int ret = ioctl(cammerFd, VIDIOC_QUERYBUF, &buf);
        if(ret < 0) 
        {
            printf("VIDIOC_QUERYBUF (%d) failed (%d)\n", i, ret);
            return ret;
        }
    
        //mmap buffer
        framebuf[i].length = buf.length;
        framebuf[i].start = (char *) mmap(NULL, buf.length, PROT_READ|PROT_WRITE, MAP_SHARED, cammerFd, buf.m.offset);
        if (framebuf[i].start == MAP_FAILED)
        {
            printf("mmap (%d) failed: %s\n", i, strerror(errno));
            return -1;
     
        }
        //queue buffer
        ret = ioctl(cammerFd, VIDIOC_QBUF, &buf);
        if (ret < 0) 
        {
			for (i=0; i< 4; i++) 
			{
				munmap(framebuf[i].start, framebuf[i].length);
				
			}
			close(cammerFd);
			printf("Camera test Done.\n");
			
			convert_rgb_to_jpg_exit();
			perror("qbuf");
			perror("qbuf");
            printf("VIDIOC_QBUF (%d) failed (%d)\n", i, ret);
            return -1;       
        }

		printf("Frame buffer %d: address=0x%x, length=%d\n", i, (unsigned int)framebuf[i].start, framebuf[i].length);
    }
	
	enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	/*struct sockaddr_in scmaddr;
	scmaddr.sin_family = AF_INET;
	scmaddr.sin_port = htons(4567);
	scmaddr.sin_addr.s_addr = inet_addr("192.168.1.207");
	
	int scmfd = socket(PF_INET,SOCK_STREAM,0);
	int ret = bind(scmfd,(struct sockaddr *)&scmaddr,sizeof(scmaddr));
	if(ret < 0)
	{
		printf("bind scmfd error\n");
		return -1;
	}
	listen(scmfd, 5);
	int scmfds = accept(scmfd,NULL,0);
	
*/
	
	u8 *jpg = NULL;
	rgb24.len = w*h*3;
	rgb24.buf = (u8 *)malloc(rgb24.len);
	assert(NULL != rgb24.buf);

	while(1){
		int ret = ioctl(cammerFd, VIDIOC_STREAMON, &type);
		if (ret < 0)
		{
			 for (i=0; i< 4; i++) 
			{
				munmap(framebuf[i].start, framebuf[i].length);
				
			}
			close(cammerFd);
			printf("Camera test Done.\n");
			
			convert_rgb_to_jpg_exit();
			printf("VIDIOC_STREAMON failed (%d)\n", ret);
			return ret;
			
		}
		sleep(0.5);
		// 获得图像
		ret = ioctl(cammerFd, VIDIOC_DQBUF, &buf);
		if (ret < 0) 
		{
			perror("DQBUF:");
			printf("VIDIOC_DQBUF failed (%d)\n", ret);
			return ret;
		}
		
		printf("*****\n");

		

		printf("buf = %d\n",sizeof(buf));
		//convert_yuv_to_rgb(yuv, rgb24.buf, w, h, bps);
		convert_yuv_to_rgb(framebuf[buf.index].start, rgb24.buf, w, h, bps);
		
		printf("111111111\n");
		convert_rgb_to_jpg_init();
		
		printf("2222222222\n");
		
		
		FILE *fp = fopen("frame1.jpg", "wb");
		if (fp < 0) 
		{
			 for (i=0; i< 4; i++) 
			{
				munmap(framebuf[i].start, framebuf[i].length);
				
			}
			close(cammerFd);
			printf("Camera test Done.\n");
		
			convert_rgb_to_jpg_exit();
			printf("open frame data file failed\n");
			return -1;
        
		}
    
		int size =  convert_rgb_to_jpg_work(rgb24.buf,fp, w, h, 80);
		fclose(fp);
		printf("Capture one frame saved in %s\n", "frame.jpg");
	   
	   int ffp = open("frame1.jpg", O_RDONLY);
		int num2 = lseek(ffp,0,SEEK_END);
		printf("num2 = %d\n",num2);
		
		char buff[num2];
		lseek(ffp,0,SEEK_SET);
		ret = read(ffp,buff,num2);
		if(ret < 0)
		{
			perror("read:");
		}
		
		char buff2[num2+8];
		sprintf(buff2,"%d*",num2);
		strcat(buff2,buff);
		
		ret = send(cfd, buff2, num2,0);
		if(ret<0)
		{
			perror("send  picture faile");
			return  -1;
		} 
		printf("send ok++++++\n");
	   
	   
		int num = rgbtojpeg(&jpg, rgb24.buf, w, h);
		printf("num =%d\n",num);
		//fp = fopen("frame.jpg", "r");
		//发送
		//printf("strlen(jpg) = %d \n",strlen(jpg));
		//char buffer[] ="hello";
		//while(ret<num)
		//{
		//ret = sendto(udpSfd,jpg,num,0,(struct sockaddr*)seraddr,sizeof(struct sockaddr_in));
		ret = send(cfd, jpg, num,0);
		if(ret<0)
		{
			perror("send faile");
			return  -1;
		}
		
	
		
		printf("33333333333\n");
		
		ret = ioctl(cammerFd, VIDIOC_QBUF, &buf);
		if (ret < 0) 
		{
			 for (i=0; i< 4; i++) 
			{
				munmap(framebuf[i].start, framebuf[i].length);
				
			}
			close(cammerFd);
			printf("Camera test Done.\n");
			
			convert_rgb_to_jpg_exit();	
			printf("VIDIOC_QBUF (%d) failed (%d)\n", i, ret);
			return -1;       
		}

		
	}
	//回收资源
    for (i=0; i< 4; i++) 
    {
        munmap(framebuf[i].start, framebuf[i].length);
        
    }
    close(cammerFd);
    printf("Camera test Done.\n");
	
	convert_rgb_to_jpg_exit();
	
	
	
	
	return 0;
}
